package com.example.teamtribe.main.club.clubintroduce.media;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseFragment;
import com.example.teamtribe.database.Media;
import com.example.teamtribe.event.EventTeam;
import com.example.teamtribe.event.Refresh;
import com.example.teamtribe.main.club.clubintroduce.GridSpacingItemDecoration;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by lenovo on 2018/5/4.
 */

public class VideoFragment extends BaseFragment implements MediaContract.View {
    private RecyclerView recyclerView;
    private MediaGridAdapter adapter;
    private int teamId;
    private MediaContract.Presenter presenter;
    private RefreshLayout refreshLayout;

    @Override
    protected int setContentView() {
        return R.layout.fragment_club_media;
    }

    @Override
    protected void onCreateView(View view) {
        presenter = new MediaPresenter(this);
        if(!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
        recyclerView = view.findViewById(R.id.rv_media_list);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, 50, true));
        recyclerView.setNestedScrollingEnabled(false);
        adapter=new MediaGridAdapter(teamId,2);
        recyclerView.setAdapter(adapter);
        presenter.getMedias(teamId,2);
        refreshLayout = (RefreshLayout)view.findViewById(R.id.rl_album);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.refreshMedias(teamId,2);
            }
        });
        refreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
    }

    @Override
    public void addMedia(Media media) {
        adapter.addMedia(media);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void setMedias(List<Media> medias) {
        adapter.setMediaList(medias);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void setCover() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void clearMedias() {
        adapter.clear();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void finishRefresh() {
        refreshLayout.finishRefresh(0);//传入false表示刷新失败
    }

    @Override
    public void next() {

    }
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void setIntroduce(EventTeam eventTeam) {
        teamId=eventTeam.getTeam().getRealId();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(Refresh r) {
        if(r.getType()==Refresh.VIDEOFRAGMENT){
            presenter.refreshMedias(teamId,2);
            Log.d("refresh", "VideoFragment-refresh: ");
        }

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
